package main

import (
	"fmt"
	"net"
)

func main() {
	//获得连接
	conn, err := net.Dial("tcp", "localhost:8000")
	if err != nil {
		fmt.Println("net.Dial err = ", err)
		return
	}
	defer conn.Close()
	//创建go协程发送消息
	go func() {
		for {
			var parse string
			fmt.Scan(&parse)
			_, err := conn.Write([]byte(parse))
			if err != nil {
				fmt.Println("conn.Write err = ", err)
				return
			}
		}
	}()
	//创建切片缓冲
	var buf = make([]byte, 1024)
	//循环接收消息
	for {
		n, err1 := conn.Read(buf)
		if n == 0 {
			fmt.Println("EXIT:", err1)
			return
		}
		//读取消息
		fmt.Println("buf = ", string(buf[:n]))
	}

}
